<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    div{
      position: absolute;
      width: 100px;
      height: 100px;
      background-color: #ccc;
    }
    span{
      display: inline-block;
      position: absolute;
      top: 120px;
      width: 50px;
      height: 50px;
      background-color: pink;
    }
  </style>
</head>
<body>
  <button>按钮</button>
  <div></div>
  <span></span>

  <script>
    function animate(obj, target) {
      clearInterval(obj.timer)

      obj.timer = setInterval(function() {  
        var step = (target - obj.offsetLeft) / 10

        if( obj.offsetLeft == target ) {
          clearInterval(timer)
        }

        obj.style.left = obj.offsetLeft + step + 'px'
      }, 30)
    }

    var div = document.querySelector('div')
    var span = document.querySelector('span')

    animate(div, 1000)

    var btn = document.querySelector('button')
    btn.onclick = function() {
      animate(span, 1000)
    }
  
    

  </script>
</body>
</html>